using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel;
using System.Drawing;

namespace TR0217.ControlEx
{
    [ToolboxBitmap(typeof(DateTimePicker))]
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip/* | ToolStripItemDesignerAvailability.StatusStrip*/)]
    public class ToolStripDateTimePicker : ToolStripControlHost
    {
        public ToolStripDateTimePicker()
            : base(CreateControlInstance())
        {
            this.AutoSize = false;
            this.Width = 110;
        }

        public ToolStripDateTimePicker(string name)
            : this()
        {
            base.Name = name;
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public DateTimePicker DateTimePicker
        {
            get { return this.Control as DateTimePicker; }
        }

        [Browsable(true)]
        public DateTime Value
        {
            get { return DateTimePicker.Value; }
            set { DateTimePicker.Value = value; }
        }

        [Browsable(false)]
        [Bindable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }


        // Subscribe and unsubscribe the control events you wish to expose.
        protected override void OnSubscribeControlEvents(Control c)
        {
            // Call the base so the base events are connected.
            base.OnSubscribeControlEvents(c);

            // Cast the control to a MonthCalendar control.
            DateTimePicker dateTimePicker = (DateTimePicker)c;

            // Add the event.
            dateTimePicker.ValueChanged +=new EventHandler(this.OnValueChanged);
        }

        protected override void OnUnsubscribeControlEvents(Control c)
        {
            // Call the base method so the basic events are unsubscribed.
            base.OnUnsubscribeControlEvents(c);

            // Cast the control to a MonthCalendar control.
            DateTimePicker dateTimePicker = (DateTimePicker)c;

            // Remove the event.
            // Add the event.
            dateTimePicker.ValueChanged -= new EventHandler(this.OnValueChanged);
        }

        public EventHandler ValueChanged;

        private void OnValueChanged(object sender, EventArgs e)
        {
            if (ValueChanged != null)
            {
                ValueChanged(this, e);
            }
        }

        private static Control CreateControlInstance()
        {
            DateTimePicker dataTimePicker = new DateTimePicker();
            dataTimePicker.Width = 110;
            return dataTimePicker;
        }
    }
}
